const Basic = require('./basic')
// const Op = require('sequelize').Op

class User extends Basic {
  constructor(sequelize, userModel, BookModel) {
    super(sequelize)
    this.userModel = userModel
    this.bookModel = BookModel
  }
  async create (params) { // 创建一条数据
    // let params = {
    //   userName: "sd",
    //   phone: 123,
    //   userId: v4(),
    //   password: 123
    // }
    let result = await this.createByModel(this.userModel, params)
    return result
  }
  async bulkCreate (params) { // 创建多条数据
    // let params = [
    //   {
    //     userName: "sd1",
    //     phone: 1234,
    //     userId: v4(),
    //     password: 123
    //   },
    //   {
    //     userName: "sd2",
    //     phone: 1235,
    //     userId: v4(),
    //     password: 123
    //   }
    // ]
    let result = await this.bulkCreateByModel(this.userModel, params)
    return result
  }
  async findOne (conditions) { // 查询一条数据
    // let conditions = {
    //   where: {
    //     userName: "zhangsan"
    //   }
    // }
    const result = await this.findOneByModel(this.userModel, conditions)
    return result
  }
  async findOrCreate (conditions) { // 查询或创建一条数据
    // const conditions = {
    //   where: {
    //     userName: 'xuwang002',
    //     phone: 123,
    //     password: 123
    //   },
    //   defaults: { // 默认值
    //     userId: uuidv4()
    //   }
    // }
    const result = await this.findOrCreateByModel(this.userModel, conditions)
    return result
  }
  async findAll (conditions) { // 查询所有数据
    // let conditions = {
    //   include: [
    //     {
    //       model: this.bookModel,
    //       as: 'bookList'
    //       // 'where': {
    //       //   'id': '5c9c5e85-4e58-4d44-aa6d-97b04c057577'
    //       // }
    //     }
    //   ],
    //   order: [
    //     ['createTime', 'DESC'], // 自身降序
    //     [{ model: this.bookModel, as: 'bookList' }, 'createTime', 'DESC'] // 子模型降序
    //   ]
    // }
    const result = await this.findAllByModel(this.userModel, conditions)
    return result
  }
  async findAndCountAll (conditions) { // 查询数据并返回数目
    // const conditions = {
    //   where: { userName: { [Op.like]: 'xuwang%' } },
    //   offset: 0,
    //   limit: 2
    // }
    const result = await this.findAndCountAllByModel(this.userModel, conditions)
    return result
  }
  async update (params, conditions) { // 更新所有满足条件的选项
    // const params = {
    //   userType: 1,
    // }
    // const conditions = {
    //   where: {
    //     userId: '123'
    //   }
    // }
    let result = await this.updateByModel(this.userModel, params, conditions)
    return result
  }
  async destroy (condtions) { // 删除所有的匹配项
    // const condtions = {
    //   where: {
    //     userName: 'xuwang001'
    //   }
    // }
    let result = await this.destroyByModel(this.userModel, condtions)
    return result
  }
  async drop () { // 清空user表
    let result = await this.dropByModel(this.userModel)
    return result
  }
}

module.exports = User